package com.uzykj.modules.blog.service;

import com.uzykj.common.utils.CacheUtils;
import com.uzykj.common.utils.StringUtils;
import com.uzykj.core.service.TreeService;
import com.uzykj.modules.blog.entity.BlogCategory;
import com.uzykj.modules.blog.mapper.BlogCategoryMapper;
import com.uzykj.modules.iim.entity.ChatPage;
import com.uzykj.modules.sys.utils.UserUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ghostxbh
 * @date 2020/7/8
 * @description 类别Service
 */
@Service
@Transactional(readOnly = true)
public class BlogCategoryService extends TreeService<BlogCategoryMapper, BlogCategory> {

    public BlogCategory get(String id) {
        return super.get(id);
    }

    public ChatPage<BlogCategory> findPage(ChatPage<BlogCategory> page, BlogCategory entity) {
        entity.setPage(page);
        page.setList(mapper.findList(entity));
        return page;
    }

    public List<BlogCategory> findList(BlogCategory category) {
        if (StringUtils.isNotBlank(category.getParentIds())) {
            category.setParentIds("," + category.getParentIds() + ",");
        }
        return super.findList(category);
    }

    public List<BlogCategory> findAllList() {
        BlogCategory category = new BlogCategory();
        category.setSort(null);
        List<BlogCategory> categoryList = super.findList(category);
        return categoryList;
    }

    @Transactional(readOnly = false)
    public void save(BlogCategory category) {
        super.save(category);
        List<BlogCategory> allList = this.findAllList();
        this.syncCacheList(allList);
    }

    @Transactional(readOnly = false)
    public void delete(BlogCategory category) {
        super.delete(category);
        List<BlogCategory> allList = this.findAllList();
        this.syncCacheList(allList);
    }

    public List<BlogCategory> getChildren(String parentId, String userId) {
        return mapper.getChildren(parentId, userId);
    }

    private void syncCacheList(List<BlogCategory> list) {
        CacheUtils.put(UserUtils.BLOG_CACHE, UserUtils.CACHE_BLOG_LIST, list);
    }
}
